package com.overseasShopping.Action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.overseasShopping.Biz.UserBiz;
import com.overseasShopping.pojo.DetailUser;
import com.overseasShopping.pojo.User;
import com.overseasShopping.util.ListUtil;

public class UserAction extends ActionSupport {
	private String userId;
	private String telephone;
	private String email;
	private String password;
	private String str;
	private String getUserId;

	public void setGetUserId(String getUserId) {
		this.getUserId = getUserId;
	}

	private UserBiz userBiz;

	public UserBiz getUserBiz() {
		return userBiz;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public void setUserBiz(UserBiz userBiz) {
		this.userBiz = userBiz;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String showFriends() throws Exception {
		List<DetailUser> frienddeDetailUsers = userBiz.getFriends(userId);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/");
		PrintWriter out = response.getWriter();
		if (frienddeDetailUsers.size() > 0) {
			for (int i = 0; i < frienddeDetailUsers.size(); i++) {
				String path = realpath + "uImg/"
						+ frienddeDetailUsers.get(i).getUserId() + ".jpg";
				File file = new File(path);

				FileOutputStream os = new FileOutputStream(file);
				InputStream img = frienddeDetailUsers.get(i).getUimage()
						.getBinaryStream();
				byte[] b = new byte[img.available()];
				img.read(b);
				os.write(b);
				os.flush();
				os.close();
			}

		}
		Map<String, Object> map = ActionContext.getContext().getSession();
		map.put("friends", frienddeDetailUsers);
		out.print(frienddeDetailUsers);
		out.flush();
		out.close();
		return SUCCESS;
	}

	public String showBlackList() throws Exception {
		List<DetailUser> blackList = userBiz.getBlackList(userId);
		String realpath = ServletActionContext.getServletContext().getRealPath(
				"/");
		if (blackList.size() > 0) {
			for (int i = 0; i < blackList.size(); i++) {
				String path = realpath + "uImg/" + blackList.get(i).getUserId()
						+ ".jpg";
				File file = new File(path);
				FileOutputStream os = new FileOutputStream(file);
				InputStream img = blackList.get(i).getUimage()
						.getBinaryStream();
				byte[] b = new byte[img.available()];
				img.read(b);
				os.write(b);
				os.flush();
				os.close();
				Map<String, Object> map = ActionContext.getContext()
						.getSession();
				map.put("blackList", blackList);
			}
		}
		return SUCCESS;
	}

	// 确认信息是否正确
	public String confirmation() throws Exception {
		String[] strs = str.split("_");
		String uid = strs[1];
		String telephone = strs[2];
		String email = strs[3];
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		User user = userBiz.getUser(uid);
		if (user == null) {
			out.write("error");
		} else {
			DetailUser detailUser = user.getDetailUser();
			if (detailUser.getTelephone().equals(telephone)
					&& detailUser.getEmail().equals(email)) {
				out.write("success");
			} else {
				out.write("fail");
			}
		}
		return null;

	}

	// 修改密码,成功后跳转到首页
	public String resetPassword() throws Exception {
		User user = userBiz.getUser(userId);
		System.out.println(userId + "____" + password);
		user.setPassword(password);
		User newUser = userBiz.updatePassword(user);
		Map<String, Object> map = ActionContext.getContext().getSession();
		map.put("user", newUser);
		return SUCCESS;
	}

	// 从黑名单添加到好友
	public String addfriend() throws Exception {
		Map<String, Object> map = ActionContext.getContext().getSession();
		User user = (User) map.get("user");
		userBiz.addFriend(user.getUserId(), userId);
		userBiz.deleteBlackUser(user.getUserId(), userId);
		return null;
	}

	// 添加好友02
	public void addFriend() {
		String Msg = "";
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/plain;charset=UTF-8");
		String fid = request.getParameter("fid");

		Map<String, Object> map = ActionContext.getContext().getSession();
		User user = (User) map.get("user");
		

		List<User> friends = ListUtil.getInstance().setToList(user.getFriends());
		
        System.err.println(user.getFriends());
        
		boolean flag = true;
		
		for (int i = 0; i < friends.size(); i++) {
			if (friends.get(i).getUserId().equals(fid)) {
				flag = false;
				break;
			}
		}

		if (user == null) {
			Msg = "尊敬的用户，您没有登陆，不可以进行该操作";
		} else {

			if (!fid.equals(user.getUserId())) {
				if (!flag) {
					Msg = "该用户已是你的好友，无须再次添加";
				} else {
					userBiz.addFriend(user.getUserId(), fid);
					Msg = "添加成功！";
                    map.put("user", userBiz.getUser(user.getUserId()));
				}
			} else {
				Msg = "不能加自己爲好友";
			}
		}
		try {
			PrintWriter out = response.getWriter();
			out.print(Msg);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 删除好友
	public String deletefriend() throws Exception {
		Map<String, Object> map = ActionContext.getContext().getSession();
		User user = (User) map.get("user");
		userBiz.deleteFriend(user.getUserId(), userId);
		return null;
	}

	// 从好友拉入黑名单
	public String addBlackUser() throws Exception {
		Map<String, Object> map = ActionContext.getContext().getSession();
		User user = (User) map.get("user");
		userBiz.deleteFriend(user.getUserId(), userId);
		userBiz.addBlackList(user.getUserId(), userId);
		return null;
	}

	// 移除黑名单
	public String deleteBlackUser() throws Exception {
		System.out.println("deletblacklist.....");
		Map<String, Object> map = ActionContext.getContext().getSession();
		User user = (User) map.get("user");
		userBiz.deleteBlackUser(user.getUserId(), userId);
		return null;
	}

	// 判定该用户是否存在
	public String sureUser() throws Exception {
		User user = userBiz.getUser(getUserId);
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter pw = response.getWriter();
		if (user != null) {
			pw.write(SUCCESS);
		}
		pw.flush();
		pw.close();
		return null;
	}

	// 注销
	public String quit() throws Exception {
		Map<String, Object> map = ActionContext.getContext().getSession();
		map.put("user", null);
		return SUCCESS;
	}
}
